/*
 * Util.java
 * business
 *
 * Created by SunDing on 2016/9/29.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.utils;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;

import com.yidiandao.business.data.bean.FileTraversal;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class LocalPictureUtil {

    Context context;

    public LocalPictureUtil(Context context) {
        this.context = context;
    }

    public ArrayList<String> listAllDir() {
        Intent intent =
                new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        Uri uri = intent.getData();
        ArrayList<String> list = new ArrayList<>();
        String[] proj = {MediaStore.Images.Media.DATA};
        Cursor cursor = context.getContentResolver().query(uri, proj, null, null, null);
        //managedQuery(uri, proj, null, null, null);
        while (cursor.moveToNext()) {
            String path = cursor.getString(0);
            list.add(new File(path).getAbsolutePath());
        }
        cursor.close();
        return list;
    }

    public List<FileTraversal> LocalImgFileList() {
        List<FileTraversal> data = new ArrayList<>();
        String filename = "";
        List<String> allimglist = listAllDir();
        List<String> retulist = new ArrayList<>();
        if (allimglist != null) {
            Set set = new TreeSet();
            String[] str;
            for (int i = 0; i < allimglist.size(); i++) {
                retulist.add(getFileInfo(allimglist.get(i)));
            }
            for (int i = 0; i < retulist.size(); i++) {
                set.add(retulist.get(i));
            }
            str = (String[]) set.toArray(new String[0]);
            for (String aStr : str) {
                filename = aStr;
                FileTraversal ftl = new FileTraversal();
                ftl.filename = filename;
                ftl.isSelected = 0;
                data.add(ftl);
            }

            for (int i = 0; i < data.size(); i++) {
                for (int j = 0; j < allimglist.size(); j++) {
                    if (data.get(i).filename.equals(getFileInfo(allimglist.get(j)))) {
                        data.get(i).filecontent.add(allimglist.get(j));
                    }
                }
            }
        }

        //添加第一行的数据
        FileTraversal ftl = new FileTraversal();
        ftl.isSelected = 0;
        ftl.filename = "所有图片";
        for (int i = 0; i < data.size(); i++) {
            ftl.filecontent.addAll(data.get(i).filecontent);
        }
        data.add(0, ftl);
        return data;
    }

    public Bitmap getPathBitmap(Uri imageFilePath, int dw, int dh) throws FileNotFoundException {
        BitmapFactory.Options op = new BitmapFactory.Options();
        op.inJustDecodeBounds = true;
        Bitmap pic =
                BitmapFactory.decodeStream(
                        context.getContentResolver().openInputStream(imageFilePath), null, op);

        int wRatio = (int) Math.ceil(op.outWidth / (float) dw);
        int hRatio = (int) Math.ceil(op.outHeight / (float) dh);

        if (wRatio > 1 && hRatio > 1) {
            if (wRatio > hRatio) {
                op.inSampleSize = wRatio;
            } else {
                op.inSampleSize = hRatio;
            }
        }
        op.inJustDecodeBounds = false;
        pic =
                BitmapFactory.decodeStream(
                        context.getContentResolver().openInputStream(imageFilePath), null, op);

        return pic;
    }

    public String getFileInfo(String data) {
        String filename[] = data.split("/");
        if (filename != null) {
            return filename[filename.length - 2];
        }
        return null;
    }
}
